package com.pipi.biyesheji.service.impl;

import com.pipi.biyesheji.dao.manager.OrderitemManager;
import com.pipi.biyesheji.dao.model.OrderitemDO;
import com.pipi.biyesheji.dao.query.OrderitemQuery;
import com.pipi.biyesheji.dao.support.PageResult;
import com.pipi.biyesheji.pojo.Order;
import com.pipi.biyesheji.pojo.OrderItem;
import com.pipi.biyesheji.pojo.Product;
import com.pipi.biyesheji.service.OrderItemService;
import com.pipi.biyesheji.service.ProductService;
import com.pipi.biyesheji.util.BeanUtils;
import com.pipi.biyesheji.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author pipi
 */
@Service
public class OrderItemServiceImpl implements OrderItemService {

    @Autowired
    private OrderitemManager orderitemManager;
    @Autowired
    ProductService productService;

    @Override
    public void save(OrderItem orderItem) {
        OrderitemDO orderitemDO = BeanUtils.createCloneBean(orderItem, OrderitemDO.class);
        orderitemManager.insertSelective(orderitemDO);
        orderItem.setId(orderitemDO.getId());
    }

    @Override
    public void del(Long id) {
        orderitemManager.deleteByPrimaryKey(id);
    }

    @Override
    public void update(OrderItem orderItem) {
        OrderitemDO orderitemDO = BeanUtils.createCloneBean(orderItem, OrderitemDO.class);
        orderitemManager.updateByPrimaryKeySelective(orderitemDO);
    }

    @Override
    public OrderItem get(Long id) {
        OrderitemDO orderitemDO = orderitemManager.selectByPrimaryKey(id);
        OrderItem orderItem = buildOrderItem(orderitemDO);
        setProduct(orderItem);
        return orderItem;
    }

    @Override
    public PageResult<OrderItem> list(Page page) {
        OrderitemQuery orderitemQuery = new OrderitemQuery();
        orderitemQuery.setOrderByClause("id asc");
        List<OrderitemDO> orderitemDOS = orderitemManager.selectByQuery(orderitemQuery);
        return PageResult.createPageResult(buildOrderItems(orderitemDOS));

    }

    @Override
    public void fill(List<Order> os) {
        for (Order o : os) {
            fill(o);
        }
    }

    /*
    1. 根据订单id查询出其对应的所有订单项
    2. 通过setProduct为所有的订单项设置Product属性
    3. 遍历所有的订单项，然后计算出该订单的总金额和总数量
    4. 最后再把订单项设置在订单的orderItems属性上。
     */
    @Override
    public void fill(Order o) {
        OrderitemQuery orderitemQuery = new OrderitemQuery();
        orderitemQuery.setOrderByClause("id asc");
        orderitemQuery.createCriteria().andOidEqualTo(o.getId());
        //查出当前订单下的所有订单项
        List<OrderitemDO> orderitemDOS = orderitemManager.selectByQuery(orderitemQuery);
        List<OrderItem> orderItems = buildOrderItems(orderitemDOS);
        //现在当前订单的每个订单项都有商品属性
        setProduct(orderItems);

        //该订单总金额
        float total = 0;
        //该订单总数
        int totalNumber = 0;
        for (OrderItem oi : orderItems) {
            //数量 * 单价
            total += oi.getNumber() * oi.getProduct().getPrice();
            totalNumber += oi.getNumber();
        }
        //设置总金额
        o.setTotal(total);
        o.setTotalNumber(totalNumber);
        //当前订单的订单项
        o.setOrderItems(orderItems);

    }

    //给当前订单下每个订单项设置商品
    private void setProduct(List<OrderItem> ois) {
        for (OrderItem oi : ois) {
            setProduct(oi);
        }
    }

    //给订单项设置商品
    private void setProduct(OrderItem oi) {
        Product p = productService.get(oi.getPid());
        oi.setProduct(p);
    }

    @Override
    public List<OrderItem> listByCustomer(Long cstid) {
        //根据用户id查询orderitem表中oid为空的集合
        OrderitemQuery orderitemQuery = new OrderitemQuery();
        orderitemQuery.createCriteria().andCstidEqualTo(cstid).andOidIsNull();
        List<OrderitemDO> orderitemDOS = orderitemManager.selectByQuery(orderitemQuery);
        List<OrderItem> orderItems = buildOrderItems(orderitemDOS);
        setProduct(orderItems);
        return orderItems;
    }


    private List<OrderItem> buildOrderItems(List<OrderitemDO> orderitemDOS) {
        return orderitemDOS.stream().map(this::buildOrderItem).collect(Collectors.toList());
    }

    private OrderItem buildOrderItem(OrderitemDO orderitemDO) {
        if (Objects.isNull(orderitemDO)) {
            return null;
        }
        return BeanUtils.createCloneBean(orderitemDO, OrderItem.class);
    }
}
